<?php
/**
*  链表的实现
*/
class LinkNode
{
  public $data;
  public $next = null;
  public function __construct($data)
  {
    $this->data = $data;
  }

  // 返回下一个节点对象
  function getNext() {
    return $this->next;
  }
  function add(&$linkNode) {
    $this->next = $linkNode;
  }
}

function createLinkNode() {
  $firstLink = null;
  // 输入高位信息
  do {
    fwrite(STDOUT, 'Input the high wei number in: 1-9 and -1 exit');
    $value = fgets(STDIN);
    if ($value >= 1 && $value <=9) {
      $firstLink = new LinkNode($value);
    }
    if ($value == -1) {
      break;
    }
  }while($value < 1 || $value > 9);

  if ($firstLink) {
    echo $firstLink->data;
    var_dump($firstLink->next);
  }

  $p = $firstLink;
  do {
    fwrite(STDOUT, 'Input a number in 1-9 and -1 exit');
    $value = fgets(STDIN);
    if ($value >= 1 && $value <= 9) {
      $linkNode = new LinkNode($value);
      $p->add($linkNode);
      // $p->next = $linkNode;
      $p = $linkNode;
    }elseif($value == -1){
      break;
    }else {
      continue;
    }
  }while($value != -1);

  $p = $firstLink;
  echo $p->data;
  while ($p->next) {
    $nextNode = $p->next;
    echo $nextNode->data;
    $p = $nextNode;
  }
  return $firstLink;
}

echo 'the first linkList:';
echo "\n";
$first = createLinkNode();
echo 'the second linkList';
echo "\n";
$second = createLinkNode();
var_dump($first);
var_dump($second);






function testInput() {
  $inputs = [];
  do {
    fwrite(STDOUT, 'please in put a number , -1 exit');
    $value = fgets(STDIN);
    if ($value != -1) {
      array_push($inputs, $value);
    }
  }while($value != -1);

  var_dump($inputs);
} 
?>